# # # import threading
# # # from queue import Queue
# # #
# # #
# # #
# # # def get_text():
# # #     i = q.get()
# # #     print('tttttttt, %s' % i)
# # #     q.task_done()
# # #
# # # def sender():
# # #     for i in range(5):
# # #         q.put(i)
# # #         print('sssss')
# # #         q.join()
# # #
# # #
# # # a = threading.Thread(target=get_text)
# # # b = threading.Thread(target=sender)
# # # q = Queue()
# # # a.start()
# # # b.start()
# #
# #
# # import pprint
# # count = {}
# # message = 'It was a bright cold day in April, and the clocks were striking thirteen.'
# # for character in message:
# #     count.setdefault(character,0)
# #     count[character] = count[character] +1
# # pprint.pprint(count)
#
# from flask import Flask,request,render_template
# from geventwebsocket.handler import WebSocketHandler
# from gevent.pywsgi import WSGIServer
# import websockets.server
# from geventwebsocket.websocket import WebSocket
# import json
#
#
# app = Flask(__name__)
#
# user_dict = {}  # 空字典,用来存放用户名和发送消息
#
# @app.route("/<username>")  # 参数为用户名
# def index(username):
#     # 获取请求的WebSocket对象
#     user_socket = request.environ.get("wsgi.websocket") # type:WebSocket
#     if user_socket:
#         # 设置键值对
#         # {'xiao': <geventwebsocket.websocket.WebSocket object at 0x0000020F6F6B8DB0>}
#         user_dict[username] = user_socket
#         print(user_dict)
#
#     # 循环,接收消息
#     while True:
#         # 接收消息
#         msg = user_socket.receive()
#         # print(msg)
#         # 反序列化数据,因为前端发送的是json
#         recv_msg = json.loads(msg)
#         print(recv_msg)
#         # 构造数据结构
#         send_msg = {
#             # 消息
#             "msg": recv_msg.get("msg"),
#             # 来自于哪个用户
#             "from_user": username,
#         }
#         # 获取聊天对象的名字
#         to_user = user_dict.get(recv_msg.get("to_user"))
#         # 发送数据
#         to_user.send(json.dumps(send_msg))
#
# @app.route("/ws")
# def ws():
#     return render_template("single_chat.html")
#
# if __name__ == '__main__':
#     # 创建一个WebSocket服务器
#     http_serv = WSGIServer(("0.0.0.0",5000),app,handler_class=WebSocketHandler)
#     # 开始监听HTTP请求
#     http_serv.serve_forever()
#     # app.run("0.0.0.0", 5000, debug=True)
#




import socket
import struct
import hashlib
import base64


def get_headers(data):
    headers = {}
    data = str(data, encoding="utf-8")

    header, body = data.split("\r\n\r\n", 1)

    header_list = header.split("\r\n")

    for i in header_list:
        i_list = i.split(":", 1)
        if len(i_list) >= 2:
            headers[i_list[0]] = "".join(i_list[1::]).strip()
        else:
            i_list = i.split(" ", 1)
            if i_list and len(i_list) == 2:
                headers["method"] = i_list[0]
                headers["protocol"] = i_list[1]
    return headers


def parse_payload(payload):
    payload_len = payload[1] & 127
    if payload_len == 126:
        extend_payload_len = payload[2:4]
        mask = payload[4:8]
        decoded = payload[8:]

    elif payload_len == 127:
        extend_payload_len = payload[2:10]
        mask = payload[10:14]
        decoded = payload[14:]
    else:
        extend_payload_len = None
        mask = payload[2:6]
        decoded = payload[6:]

    # 这里我们使用字节将数据全部收集，再去字符串编码，这样不会导致中文乱码
    bytes_list = bytearray()

    for i in range(len(decoded)):
        # 解码方式
        chunk = decoded[i] ^ mask[i % 4]
        bytes_list.append(chunk)
    body = str(bytes_list, encoding='utf-8')
    return body


def send_msg(conn, msg_bytes):
    token = b"\x81"
    length = len(msg_bytes)
    if length < 126:
        token += struct.pack("B", length)
    elif length <= 0xFFFF:
        token += struct.pack("!BH", 126, length)
    else:
        token += struct.pack("!BQ", 127, length)

    msg = token + msg_bytes
    conn.sendall(msg)
    return True


def server_socket():
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    sock.bind(("127.0.0.1", 10083))
    sock.listen(5)
    conn, addr = sock.accept()
    print(conn)
    data = conn.recv(8096)
    headers = get_headers(data)
    # 对请求头中的sec-websocket-key进行加密
    response_tpl = "HTTP/1.1 101 Switching Protocols\r\n" \
                   "Upgrade:websocket\r\n" \
                   "Connection: Upgrade\r\n" \
                   "Sec-WebSocket-Accept: %s\r\n" \
                   "WebSocket-Location: ws://%s\r\n\r\n"

    magic_string = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'

    if headers.get('Sec-WebSocket-Key'):
        value = headers['Sec-WebSocket-Key'] + magic_string

    ac = base64.b64encode(hashlib.sha1(value.encode('utf-8')).digest())
    response_str = response_tpl % (ac.decode('utf-8'), headers.get("Host"))
    conn.sendall(bytes(response_str, encoding="utf-8"))

    while True:
        data_1 = conn.recv(8096)
        data_2 = parse_payload(data_1)
        print(data_2)
        send_msg(conn, b"test")


if __name__ == "__main__":
    server_socket()
